#!/bin/bash
# TestTrue实TraceData集的CompleteScript

echo "=========================================="
echo " True实TraceData集VerifyTest"
echo "=========================================="

# CheckPython环境
echo ""
echo "[1] CheckPython环境..."
python3 --version || { echo "Error: 未找ToPython3"; exit 1; }

# 安装依赖
echo ""
echo "[2] 安装必要依赖..."
pip install requests tqdm numpy scipy h5py -q

# CreateResultDirectory
mkdir -p results/real_traces_test

# TestAll合成Data集
echo ""
echo "=========================================="
echo " Test1: 合成Data集（对称Algorithm）"
echo "=========================================="

# TestSM4
echo ""
echo "--- TestSM4 ---"
python3 download_real_traces.py --dataset sm4_synthetic

if [ -f "datasets/sm4_synthetic/sm4_synthetic_traces.npz" ]; then
    echo "StartSM4AttackTest..."
    python3 analyze_firmware_traces.py \
        datasets/sm4_synthetic/sm4_synthetic_traces.npz \
        --algorithm sm4 \
        --attacks cpa \
        --leakage-model hamming_weight \
        --target-operation first_round \
        --top-n 100 \
        --report results/real_traces_test/sm4_test_report.json
    echo "✓ SM4TestCompleted"
else
    echo "✗ SM4Data集GenerateFailed"
fi

echo ""
echo "=========================================="
echo " Test2: 合成Data集（非对称Algorithm）"
echo "=========================================="

# TestRSA
echo ""
echo "--- TestRSA ---"
python3 download_real_traces.py --dataset rsa_synthetic

if [ -f "datasets/rsa_synthetic/rsa_synthetic_traces.npz" ]; then
    echo "StartRSAAttackTest..."
    python3 analyze_firmware_traces.py \
        datasets/rsa_synthetic/rsa_synthetic_traces.npz \
        --algorithm rsa \
        --attacks spa \
        --target-operation modular_exponentiation \
        --top-n 50 \
        --report results/real_traces_test/rsa_test_report.json
    echo "✓ RSATestCompleted"
else
    echo "✗ RSAData集GenerateFailed"
fi

# TestECC
echo ""
echo "--- TestECC ---"
python3 download_real_traces.py --dataset ecc_synthetic

if [ -f "datasets/ecc_synthetic/ecc_synthetic_traces.npz" ]; then
    echo "StartECCAttackTest..."
    python3 analyze_firmware_traces.py \
        datasets/ecc_synthetic/ecc_synthetic_traces.npz \
        --algorithm ecc \
        --attacks spa \
        --target-operation scalar_multiplication \
        --top-n 50 \
        --report results/real_traces_test/ecc_test_report.json
    echo "✓ ECCTestCompleted"
else
    echo "✗ ECCData集GenerateFailed"
fi

# TestSM2
echo ""
echo "--- TestSM2 ---"
python3 download_real_traces.py --dataset sm2_synthetic

if [ -f "datasets/sm2_synthetic/sm2_synthetic_traces.npz" ]; then
    echo "StartSM2AttackTest..."
    python3 analyze_firmware_traces.py \
        datasets/sm2_synthetic/sm2_synthetic_traces.npz \
        --algorithm sm2 \
        --attacks spa \
        --target-operation scalar_multiplication \
        --top-n 50 \
        --report results/real_traces_test/sm2_test_report.json
    echo "✓ SM2TestCompleted"
else
    echo "✗ SM2Data集GenerateFailed"
fi

echo ""
echo "=========================================="
echo " Test3: 合成Data集（Post-QuantumAlgorithm）"
echo "=========================================="

# TestDilithium
echo ""
echo "--- TestDilithium ---"
python3 download_real_traces.py --dataset dilithium_synthetic

if [ -f "datasets/dilithium_synthetic/dilithium_synthetic_traces.npz" ]; then
    echo "StartDilithiumAttackTest..."
    python3 analyze_firmware_traces.py \
        datasets/dilithium_synthetic/dilithium_synthetic_traces.npz \
        --algorithm dilithium \
        --attacks template \
        --target-operation ntt_transform \
        --top-n 50 \
        --report results/real_traces_test/dilithium_test_report.json
    echo "✓ DilithiumTestCompleted"
else
    echo "✗ DilithiumData集GenerateFailed"
fi

echo ""
echo "Generate综合报告..."
python3 generate_experiment_report.py results/real_traces_test/
echo "✓ All合成DataTestCompleted！请查看 results/real_traces_test/experiment_report.html"

# 询问YesNo下载DPA Contest V4
echo ""
echo "=========================================="
echo " Test4: DPA Contest V4 (Optional)"
echo "=========================================="
read -p "YesNo下载并TestDPA Contest V4Data集? (Require较长时间) [y/N]: " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    python3 download_real_traces.py --dataset dpa_v4
    
    if [ -f "datasets/dpa_contest_v4/dpa_v4_traces.npz" ]; then
        echo ""
        echo "StartAttackTest..."
        python3 analyze_firmware_traces.py \
            datasets/dpa_contest_v4/dpa_v4_traces.npz \
            --algorithm aes \
            --attacks cpa \
            --leakage-model hamming_weight \
            --target-operation first_round \
            --top-n 100 \
            --report results/real_traces_test/dpa_v4_test_report.json \
            --verbose
        
        echo ""
        echo "Generate报告..."
        python3 generate_experiment_report.py results/real_traces_test/
        
        echo ""
        echo "✓ DPA Contest V4TestCompleted！"
    else
        echo "✗ DPA Contest V4Data集ProcessFailed"
    fi
fi

echo ""
echo "=========================================="
echo " TestCompleted"
echo "=========================================="
echo ""
echo "Result总结:"
echo "  - Test报告: results/real_traces_test/experiment_report.html"
echo "  - JSON报告: results/real_traces_test/*.json"
echo ""
echo "预期Result:"
echo ""
echo "  【Symmetric EncryptionAlgorithm】"
echo "  SM4合成Data (CPAAttack):"
echo "    - Exact Match Rate: 80-100%"
echo "    - Avg Confidence: 0.45-0.65"
echo "    - P-Value: < 0.001 (高度显著)"
echo ""
echo "  【非Symmetric EncryptionAlgorithm】"
echo "  RSA合成Data (SPAAttack):"
echo "    - 应能识别平方-乘Algorithm模式"
echo "    - 可恢复私钥bit模式"
echo ""
echo "  ECC/SM2合成Data (SPAAttack):"
echo "    - 应能识别double-and-add模式"
echo "    - 可恢复Scalar倍点的bit序列"
echo ""
echo "  【Post-QuantumAlgorithm】"
echo "  Dilithium合成Data (TemplateAttack):"
echo "    - 应能识别NTT变换模式"
echo "    - 应能识别多项式乘法操作"
echo "    - 应能识别拒绝Sampling迭代"
echo "    - 可区分Not同的私钥系数"
echo ""
echo "  DPA Contest V4 (IfTest):"
echo "    - Exact Match Rate: 50-80%"
echo "    - P-Value: < 0.01 (显著)"
echo ""
echo "IfResult符合预期，DescriptionAttack系统对多种Algorithm都工作正常！"
